// JavaScript Document
function UUID(){
	var S4=function () {
		 return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
	};
	return (S4()+S4()+S4()+S4()+S4()+S4()+S4()+S4());
}

var speed=20;
var timer=0;
var toLeft=function(){
	$("#frm-nav").scrollLeft($("#frm-nav").scrollLeft()-speed);
}
var toRight=function(){
	if(($("#frm-bav-bar").width()-$("#frm-nav").width())>$("#frm-nav").scrollLeft())
		$("#frm-nav").scrollLeft($("#frm-nav").scrollLeft()+speed);
}
var stopScroll=function(){
	clearInterval(timer);
	timer=0;
}
var onSize=function(){
	//$("#iframes").height(($(window).height()-$(".main-header").height())+"px");
}
$(function(){
	$(document).on("mousedown","#toLeft",function(){
		stopScroll();
		timer=setInterval(toLeft,50);	
	});
	$(document).on("mousedown","#toRight",function(){	
		stopScroll();
		timer=setInterval(toRight,50);
	});
	$(document).on("mouseup","#toLeft,#toRight",stopScroll);
	$(document).on("mouseleave","#toLeft,#toRight",stopScroll);
	$(document).resize(onSize);
	onSize();
});

window.hrefs=[];
window.current;
var navigateTo=function(params){
	var url = params.url;
	var title = params.title;
	var icon = params.icon;
	if(url.indexOf("#")==0)
		return false;
	var flag=url;
	if(url.indexOf("?")!=-1)
	   flag=url.substring(0,url.indexOf("?"));			
	if(hrefs[flag]){
		hrefs[flag].active();
		hrefs[flag].iframe.attr("src",url);
		return true;
	}
	var frameHandle = "handle_"+UUID();
	var newHref=hrefs[flag]=new Object();
	newHref.flag=flag;
	newHref.url=url;
	newHref.title=title;
	newHref.item=$("<li><a href='javascript:void(0);'>"+title+"<i>x</i></a></li>");
	newHref.item.get(0).onwer=newHref;
	newHref.item.appendTo($("#frm-bav-bar"));
	newHref.button=newHref.item.find("a");
	newHref.icon=newHref.item.find("i");
	newHref.iframe=$("<iframe id='"+frameHandle+"' name='"+frameHandle+"' src='' scrolling='yes' width='100%' height='100%' frameborder='0' style='width:100%;height:100%;'></iframe>");
	newHref.iframe.appendTo($("#iframes"));
	try{
		newHref.iframe.load(function(){
			var contentframe = $(this).get(0);	
			if($(contentframe.contentWindow.htmlcument).size()){
				var framedocument = $(contentframe.contentWindow.htmlcument);
				if(newHref.button.text()==""){
					newHref.button.html(framedocument.find("title").html());
				}
			}
		});
	}catch(e){
		console.error(e);
	}
	
	newHref.active=function(){
		$("#frm-bav-bar li").removeClass("active");
		$("#iframes iframe").hide();
		if(window.current){
			try{
				top.$(window.current.iframe.get(0).contentWindow).trigger("visible",false);
			}catch(e){
				console.error(e);
			}
		}
		window.current=newHref;
		window.current.item.addClass("active");
		window.current.iframe.show();
		try{
			top.$(window.current.iframe.get(0).contentWindow).trigger("visible",true);	
		}catch(e){
			console.error(e);
		}
	}
	newHref.close=function(){
		delete hrefs[newHref.flag];
		if(window.current==newHref){
			if(newHref.item.prev().length>0)
				newHref.item.prev().get(0).onwer.active();
			else if(newHref.item.next().length>0)
				newHref.item.next().get(0).onwer.active();
			else{
				window.current=null;
				$("#iframes iframe").show();
			}
		}
		try{
			$(newHref.iframe.get(0).contentWindow).unload();
		}catch(e){
			console.error(e);
		}
		newHref.item.remove();
		newHref.iframe.remove();
	}
	newHref.open=function(){
		var tUrl=window.current.url;
		tUrl=tUrl+(tUrl.indexOf("?")>-1 ? "&" : "?") +"mm="+ Math.random();//防止缓存，在网址后面加随数		
		window.current.iframe.get(0).src=tUrl;		
	}
	
	newHref.button.click(newHref.active);
	newHref.icon.click(newHref.close);
	
	newHref.active();
	newHref.open();	
	
	return true;
}


function updateSidebarWarpper(){
	$(".sidebar-warpper").css("height",($(window).height()-$(".sidebar-warpper").offset().top)+"px");
}

$(function(){
	$(document).on("click",".navbar-close-btns a",function(){
		var scope = $(this).attr("scope");	
		if(scope=="current"){
			if(window.current){
				window.current.close();
			}
		}else if(scope=="other"){
			for(var o in window.hrefs){
				if(window.hrefs[o]!=window.current){
					window.hrefs[o].close();
				}
			}
		}else if(scope=="all"){
			for(var o in window.hrefs){
				window.hrefs[o].close();
			}
		}
		return true;
	});
	
	
	
	var setSkin = function(dataSkin){
		var skins = [
	        'skin-blue',
	        'skin-black',
	        'skin-red',
	        'skin-yellow',
	        'skin-purple',
	        'skin-green',
	        'skin-blue-light',
	        'skin-black-light',
	        'skin-red-light',
	        'skin-yellow-light',
	        'skin-purple-light',
	        'skin-green-light'
	    ];
		for(var o in skins)
			$("body").removeClass(skins[o]);
		$("body").addClass(dataSkin);
		if (typeof (Storage) !== 'undefined') {
            localStorage.setItem("data-skin",dataSkin);
        }
	}
	$(document).on("click",".list-unstyled a[data-skin]",function(){
		setSkin($(this).attr("data-skin"));
		return true;
	});
	
	if (typeof (Storage) !== 'undefined') {
        if(localStorage.getItem("data-skin")){
        	setSkin(localStorage.getItem("data-skin"));
        }
    }
	
	$(window).resize(updateSidebarWarpper);
	$(updateSidebarWarpper);
	$(function(){ 
		updateSidebarWarpper();
	});
	
	top.navigateTo({url:'welcome.html',title:'系统主页'});
});


